﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class AuditdefinitionBLL : IBaseInfoDetail
    {
        private static readonly IAuditDefinitionDA auditDefinitionDA = DataAccess.CreateAuditDefinitionDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;

        public AuditdefinitionBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public AuditdefinitionBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            throw new NotImplementedException();
        }

        public DataSet GetAllDetailInfo()
        {
            return auditDefinitionDA.GetAllAuditDefinition(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(object detailQuery)
        {
            AuditDefinitionQueryEntity auditDefinitionQuery = detailQuery as AuditDefinitionQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(auditDefinitionQuery.TableName))
            {
                sqlWhere.Add(" TABLE_NAME " + auditDefinitionQuery.TableName);
            }
            return auditDefinitionDA.GetAuditDefinitionByQueryList(sqlWhere, auditDefinitionQuery, this.dataBaseID[0]);
        }

        public object GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            throw new NotImplementedException();
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            List<AuditDefinitionInfo> auditDefinitionInfos = auditDefinitionDA.GetAuditDefinitionByTableName(mainID, this.dataBaseID[0]);
            List<object> objs = null;
            if ((auditDefinitionInfos != null) && (auditDefinitionInfos.Count > 0))
            {
                objs = new List<object>();
                for (int i = 0; i < auditDefinitionInfos.Count; i++)
                {
                    objs.Add(auditDefinitionInfos[i]);
                }
            }
            return objs;
        }

        public string InsertDetail(object obj)
        {
            throw new NotImplementedException();
        }

        public string UpdateDetail(object obj)
        {
            Exception ex;
            string strAuditDefinition = string.Empty;
            try
            {
                List<AuditDefinitionInfo> auditDefinitions = obj as List<AuditDefinitionInfo>;
                if ((auditDefinitions != null) && (auditDefinitions.Count > 0))
                {
                    DataBase dataBase = new DataBase();
                    DbConnection conn = dataBase.connection;
                    conn.Open();
                    DbTransaction tran = conn.BeginTransaction();
                    try
                    {
                        for (int i = 0; i < auditDefinitions.Count; i++)
                        {
                            if (auditDefinitionDA.UpdateAuditDefinition(dataBase, tran, auditDefinitions[i], this.dataBaseID[0]) > 0)
                            {
                                strAuditDefinition = strAuditDefinition + auditDefinitions[i].ColName + ",";
                            }
                        }
                        tran.Commit();
                    }
                    catch (Exception exception1)
                    {
                        ex = exception1;
                        tran.Rollback();
                        CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            catch (Exception exception2)
            {
                ex = exception2;
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return ((strAuditDefinition.Length > 0) ? strAuditDefinition.Substring(0, strAuditDefinition.Length - 1) : strAuditDefinition);
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

